package com.work.xxzl.module.pagehome;

import android.graphics.drawable.Drawable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.work.xxzl.R;
import com.work.xxzl.adapter.CommonRecyclerViewAdapter;
import com.work.xxzl.adapter.CommonRecyclerViewHolder;
import com.work.xxzl.base.BaseFragment;
import com.work.xxzl.utils.UIUtils;
import com.work.xxzl.view.DividerItemDecoration;
import com.work.xxzl.view.SwipRefreshViewCommon;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * author: Aly
 * date :2018/12/7 16:27
 * version:1.0
 * description:推荐
 */
public class SuggestFragment extends BaseFragment {
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    @BindView(R.id.swiprefreshview)
    SwipRefreshViewCommon swiprefreshview;
    private List<String> mDatas = new ArrayList<>();
    private CommonRecyclerViewAdapter<String> adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.layout_recycleview;
    }

    @Override
    protected void initView() {
        setAdapter();
    }

    @Override
    protected void initData() {
        super.initData();
        setRefresh();
    }

    private void setRefresh() {
        swiprefreshview.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swiprefreshview.setRefreshing(false);
            }
        });
    }

    //设置适配器
    private void setAdapter() {
        for (int i = 0; i < 10; i++) {
            mDatas.add("");
        }
        adapter = new CommonRecyclerViewAdapter<String>(getContext(), mDatas) {
            private ImageView iv_image,iv_level;
            private TextView tv_nickname, tv_jiaoyou, tv_zhenghun, tv_old;
            private LinearLayout lt_daoshi;

            @Override
            public void convert(CommonRecyclerViewHolder holder, String entity, int position) {
                tv_jiaoyou = holder.getView(R.id.tv_jiaoyou);
                tv_zhenghun = holder.getView(R.id.tv_zhenghun);
                iv_level = holder.getView(R.id.iv_level);
                lt_daoshi = holder.getView(R.id.lt_daoshi);
                tv_old = holder.getView(R.id.tv_old);

                iv_image = holder.getView(R.id.iv_image);
                tv_nickname = holder.getView(R.id.tv_nickname);
                tv_nickname.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                ViewGroup.LayoutParams layoutParams = iv_image.getLayoutParams();
                layoutParams.height = UIUtils.getScreenWidth() - UIUtils.dip2px(20);
                layoutParams.width = UIUtils.getScreenWidth();
                iv_image.setLayoutParams(layoutParams);
                holder.setImage(iv_image, R.mipmap.image3);
                if (position == 0 || position == 2) {
                    //交友会员
                    tv_jiaoyou.setVisibility(View.VISIBLE);
                    tv_zhenghun.setVisibility(View.GONE);
                    lt_daoshi.setVisibility(View.GONE);
                    iv_level.setImageResource(R.mipmap.vip);
                    tv_old.setText("26");
                    tv_old.setBackgroundResource(R.drawable.bghalfred_oval);
                    // 使用代码设置drawableleft
                    Drawable drawable = getResources().getDrawable(
                            R.mipmap.nv);
                    // / 这一步必须要做,否则不会显示.
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                            drawable.getMinimumHeight());
                    tv_old.setCompoundDrawables(drawable, null, null, null);
                    holder.setImage(R.id.iv_image, "http://pic31.nipic.com/20130703/7447430_140859102000_2.jpg");

                } else if (position == 1 || position == 4) {
                    //征婚会员
                    tv_jiaoyou.setVisibility(View.GONE);
                    tv_zhenghun.setVisibility(View.VISIBLE);
                    lt_daoshi.setVisibility(View.GONE);
                    iv_level.setImageResource(R.mipmap.zuanshi);
                    tv_old.setText("未婚");
                    tv_old.setBackgroundResource(R.drawable.bghalfred_oval);
                    tv_old.setCompoundDrawables(null, null, null, null);
                    holder.setImage(R.id.iv_image, "http://s10.sinaimg.cn/mw690/67e2a2dbgd8a94c2ac169&690");

                } else {
                    //导师
                    tv_jiaoyou.setVisibility(View.GONE);
                    tv_zhenghun.setVisibility(View.GONE);
                    lt_daoshi.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.jinpai);
                    tv_old.setText("联系她");
                    tv_old.setBackgroundResource(R.drawable.bghalfblue_oval);
                    tv_old.setCompoundDrawables(null, null, null, null);
                    holder.setImage(R.id.iv_image, "http://pic30.nipic.com/20130605/7447430_160624016000_2.jpg");

                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_homepage_suggestfragment;
            }
        };
        recycleview.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleview.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL, UIUtils.dip2px(10), R.color.white));
        recycleview.setAdapter(adapter);
    }
}
